﻿package com.controller;

import java.io.File;
import java.io.IOException;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Lvyougongsi;
import com.server.LvyougongsiServer;
import com.util.PageBean;
import net.sf.json.JSONObject;
import com.util.db;
import java.sql.SQLException;
import java.sql.*;
@Controller
public class LvyougongsiController {
	@Resource
	private LvyougongsiServer lvyougongsiService;


   
	@RequestMapping("addLvyougongsi.do")
	public String addLvyougongsi(HttpServletRequest request,Lvyougongsi lvyougongsi,HttpSession session) throws SQLException{
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		lvyougongsi.setAddtime(time.toString().substring(0, 19));
		lvyougongsiService.add(lvyougongsi);
		session.setAttribute("backxx", "添加成功");
		session.setAttribute("backurl", request.getHeader("Referer"));
		
		//session.setAttribute("backurl", "lvyougongsiList.do");
		
		return "redirect:postback.jsp";
		//return "redirect:lvyougongsiList.do";
		
		
		
	}
 
//	处理编辑
	@RequestMapping("doUpdateLvyougongsi.do")
	public String doUpdateLvyougongsi(int id,ModelMap map,Lvyougongsi lvyougongsi){
		lvyougongsi=lvyougongsiService.getById(id);
		map.put("lvyougongsi", lvyougongsi);
		return "lvyougongsi_updt";
	}
	
	@RequestMapping("doUpdateLvyougongsi2.do")
	public String doUpdateLvyougongsi2(ModelMap map,Lvyougongsi lvyougongsi,HttpServletRequest request){
		lvyougongsi=lvyougongsiService.getById(Integer.parseInt((String)request.getSession().getAttribute("uid")));
		map.put("lvyougongsi", lvyougongsi);
		return "lvyougongsi_updt2";
	}
	
@RequestMapping("updateLvyougongsi2.do")
	public String updateLvyougongsi2(int id,ModelMap map,Lvyougongsi lvyougongsi){
		lvyougongsiService.update(lvyougongsi);
		return "redirect:doUpdateLvyougongsi2.do";
	}
	
	
	
//	后台详细
	@RequestMapping("lvyougongsiDetail.do")
	public String lvyougongsiDetail(int id,ModelMap map,Lvyougongsi lvyougongsi){
		lvyougongsi=lvyougongsiService.getById(id);
		map.put("lvyougongsi", lvyougongsi);
		return "lvyougongsi_detail";
	}
//	前台详细
	@RequestMapping("lygsDetail.do")
	public String lygsDetail(int id,ModelMap map,Lvyougongsi lvyougongsi){
		lvyougongsi=lvyougongsiService.getById(id);
		map.put("lvyougongsi", lvyougongsi);
		return "lvyougongsidetail";
	}
//	
	@RequestMapping("updateLvyougongsi.do")
	public String updateLvyougongsi(int id,ModelMap map,Lvyougongsi lvyougongsi,HttpServletRequest request,HttpSession session){
		lvyougongsiService.update(lvyougongsi);
		session.setAttribute("backxx", "修改成功");
		session.setAttribute("backurl", request.getHeader("Referer"));
		return "redirect:postback.jsp";
		//String url = request.getHeader("Referer");
		//return "redirect:"+url;
		//return "redirect:lvyougongsiList.do";
	}

//	分页查询
	@RequestMapping("lvyougongsiList.do")
	public String lvyougongsiList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Lvyougongsi lvyougongsi, String gongsibianhao, String gongsimingcheng, String LOGO, String fuwurexian, String suozaichengshi, String gongsidizhi, String gongsijianjie, String mima, String issh){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 5);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 5);
		
		
		if(gongsibianhao==null||gongsibianhao.equals("")){pmap.put("gongsibianhao", null);}else{pmap.put("gongsibianhao", gongsibianhao);}		if(gongsimingcheng==null||gongsimingcheng.equals("")){pmap.put("gongsimingcheng", null);}else{pmap.put("gongsimingcheng", gongsimingcheng);}		if(LOGO==null||LOGO.equals("")){pmap.put("LOGO", null);}else{pmap.put("LOGO", LOGO);}		if(fuwurexian==null||fuwurexian.equals("")){pmap.put("fuwurexian", null);}else{pmap.put("fuwurexian", fuwurexian);}		if(suozaichengshi==null||suozaichengshi.equals("")){pmap.put("suozaichengshi", null);}else{pmap.put("suozaichengshi", suozaichengshi);}		if(gongsidizhi==null||gongsidizhi.equals("")){pmap.put("gongsidizhi", null);}else{pmap.put("gongsidizhi", gongsidizhi);}		if(gongsijianjie==null||gongsijianjie.equals("")){pmap.put("gongsijianjie", null);}else{pmap.put("gongsijianjie", gongsijianjie);}		if(mima==null||mima.equals("")){pmap.put("mima", null);}else{pmap.put("mima", mima);}		
		int total=lvyougongsiService.getCount(pmap);
		pageBean.setTotal(total);
		List<Lvyougongsi> list=lvyougongsiService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "lvyougongsi_list";
	}
	
	
	
	@RequestMapping("lygsList.do")
	public String lygsList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Lvyougongsi lvyougongsi, String gongsibianhao, String gongsimingcheng, String LOGO, String fuwurexian, String suozaichengshi, String gongsidizhi, String gongsijianjie, String mima, String issh){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 5);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 5);
		
		
		if(gongsibianhao==null||gongsibianhao.equals("")){pmap.put("gongsibianhao", null);}else{pmap.put("gongsibianhao", gongsibianhao);}		if(gongsimingcheng==null||gongsimingcheng.equals("")){pmap.put("gongsimingcheng", null);}else{pmap.put("gongsimingcheng", gongsimingcheng);}		if(LOGO==null||LOGO.equals("")){pmap.put("LOGO", null);}else{pmap.put("LOGO", LOGO);}		if(fuwurexian==null||fuwurexian.equals("")){pmap.put("fuwurexian", null);}else{pmap.put("fuwurexian", fuwurexian);}		if(suozaichengshi==null||suozaichengshi.equals("")){pmap.put("suozaichengshi", null);}else{pmap.put("suozaichengshi", suozaichengshi);}		if(gongsidizhi==null||gongsidizhi.equals("")){pmap.put("gongsidizhi", null);}else{pmap.put("gongsidizhi", gongsidizhi);}		if(gongsijianjie==null||gongsijianjie.equals("")){pmap.put("gongsijianjie", null);}else{pmap.put("gongsijianjie", gongsijianjie);}		if(mima==null||mima.equals("")){pmap.put("mima", null);}else{pmap.put("mima", mima);}		
		int total=lvyougongsiService.getCount(pmap);
		pageBean.setTotal(total);
		List<Lvyougongsi> list=lvyougongsiService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "lvyougongsilist";
	}
	
	@RequestMapping("deleteLvyougongsi.do")
	public String deleteLvyougongsi(int id,HttpServletRequest request){
		lvyougongsiService.delete(id);
		String url = request.getHeader("Referer");
		return "redirect:"+url;
		//return "redirect:lvyougongsiList.do";
	}
	
	@RequestMapping("quchongLvyougongsi.do")
	public void quchongLvyougongsi(Lvyougongsi lvyougongsi,HttpServletResponse response){
		   Map<String,Object> map=new HashMap<String,Object>();
		   map.put("gongsibianhao", lvyougongsi.getGongsibianhao());
		   System.out.println("gongsibianhao==="+lvyougongsi.getGongsibianhao());
		   System.out.println("gongsibianhao222==="+lvyougongsiService.quchongLvyougongsi(map));
		   JSONObject obj=new JSONObject();
		   if(lvyougongsiService.quchongLvyougongsi(map)!=null){
				 obj.put("info", "ng");
			   }else{
				   obj.put("info", "公司编号可以用！");
				  
			   }
		   response.setContentType("text/html;charset=utf-8");
		   PrintWriter out=null;
		   try {
			out=response.getWriter();
			out.print(obj);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			out.close();
		}
	}
}
